package com.vod.yn.controller;

import com.vod.yn.domain.VideoCourse;
import com.vod.yn.model.ResultModel;
import com.vod.yn.service.VideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/yn/video")
public class videoController {

    private final Logger LOG = LoggerFactory.getLogger(this.getClass()); // 日志记录对象


    @Autowired
    private VideoService videoService;

    @RequestMapping(value = "/getVideoInfo", method = RequestMethod.POST)
    public ResultModel<Object> getVideoInfo(HttpServletRequest request, Integer id) {
        ResultModel<Object> result = ResultModel.newResultModel();
        try {
            VideoCourse info = videoService.getVideoCourseInfo(id);
            result.setData(info);
            return result;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            result.setRtnCode(500);
            result.setRtnMsg("服务端异常,reason:" + e.getMessage());
            return result;
        }
    }


    @RequestMapping(value = "/addVideoInfo", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public ResultModel<Object> addVideoInfo(@RequestBody VideoCourse videoCourse) {
        ResultModel<Object> result = ResultModel.newResultModel();
        try {
            Integer res = videoService.addVideoInfo(videoCourse);
            if (res != 1) {
                result.setSuccess(false);
            } else {
                result.setSuccess(true);
            }
            return result;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            result.setRtnCode(500);
            result.setRtnMsg("服务端异常,reason:" + e.getMessage());
            return result;
        }
    }

    @RequestMapping(value = "/updateVideoInfo", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public ResultModel<Object> updateVideoInfo(@RequestBody VideoCourse videoCourse) {
        ResultModel<Object> result = ResultModel.newResultModel();
        try {
            Integer res = videoService.updateVideoInfo(videoCourse);
            if (res != 1) {
                result.setSuccess(false);
            } else {
                result.setSuccess(true);
            }
            return result;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            result.setRtnCode(500);
            result.setRtnMsg("服务端异常,reason:" + e.getMessage());
            return result;
        }
    }


}
